package com.qf.springbootdemo01.controller;

import com.github.pagehelper.PageInfo;
import com.qf.springbootdemo01.entity.ResultVO;
import com.qf.springbootdemo01.pojo.User;
import com.qf.springbootdemo01.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/19
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查全部用户
     * @return
     */
    @RequestMapping("/findAll")
    public ModelAndView findAllUser(){
        ResultVO<List<User>> vo = userService.findAllUser();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("user/user-list");
        mav.addObject("users",vo.getData());
        return mav;
    }

    /**
     * 按条件查询用户
     * @param condition
     * @return
     */
    @GetMapping("/findUserByCondition")
    public ModelAndView findUserByCondition(String condition){
        ResultVO<List<User>> vo = userService.findUserByCondition(condition);
        ModelAndView mav = new ModelAndView();
        mav.addObject("users", vo.getData());
        mav.addObject("condition", condition);
        mav.setViewName("user/user-list");
        return mav;
    }

    /**
     * 查全部用户 + 分页
     * @return
     */
    @RequestMapping("/findAllWithPage")
    //public ModelAndView findAllWithPage(Integer pageNum, Integer pageSize){
    public ResultVO findAllWithPage(Integer pageNum, Integer pageSize){
        if (null == pageNum){
            pageNum = 1;
        }
        if (null == pageSize){
            pageSize = 5;
        }
        ResultVO<PageInfo<User>> vo = userService.findAllWithPage(pageNum, pageSize);
        //ModelAndView mav = new ModelAndView();
        //mav.setViewName("user/user-list");
        //System.out.println("vo data --> " + vo.getData());
        //mav.addObject("pageUsers",vo.getData());
        return vo;
    }

    /**
     * 按条件查询用户 + 分页
     * @param condition
     * @return
     */
    @GetMapping("/findUserByConditionWithPage")
    public ModelAndView findUserByConditionWithPage(String condition, Integer pageNum, Integer pageSize){
        if (null == pageNum){
            pageNum = 1;
        }
        if (null == pageSize){
            pageSize = 5;
        }
        ResultVO<PageInfo<User>> vo = userService.findUserByConditionWithPage(condition, pageNum, pageSize);
        ModelAndView mav = new ModelAndView();
        mav.addObject("pageUsers", vo.getData());
        mav.addObject("condition", condition);
        mav.setViewName("user/user-list");
        return mav;
    }

    /**
     * 根据id查用户
     * @param id
     * @return
     */
    @GetMapping("/findUserById/{userId}")
    @ResponseBody
    public ResultVO findUserById(@PathVariable("userId") Integer id){
        ResultVO vo = userService.findUserById(id);
        return vo;
    }

    /**
     * 根据id删除用户
     * @param userId
     * @return
     */
    @PutMapping("/deleteUserById/{userId}")
    @ResponseBody
    public ResultVO deleteUserById(@PathVariable("userId") Integer userId){
        return userService.deleteUserById(userId);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    @ResponseBody
    public ResultVO saveUser(@RequestBody User user){
        ResultVO vo = userService.saveUser(user);
        vo.setData(user);
        return vo;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    @ResponseBody
    public ResultVO updateUser(User user){
        System.out.println(user);
        return userService.updateUser(user);
    }

    @PostMapping("/login")
    public ResultVO<Map> login(@CookieValue String token, User user){
        log.info("token:{}", token);
        log.info("user:{}", user);
        //处理逻辑

        Map map = new HashMap();
        map.put("token", token);
        map.put("user", user);
        return ResultVO.ok("登陆成功~", map);
    }

}
